package com.zp.resource.user;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.user.PermissionVo;
import com.zp.bean.user.ResultPermissionVo;
import com.zp.domain.Permission;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * permission services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/12
 * 
 */
@Component
@Path("/rest/permission")
@Singleton
public class PermissionServices extends BaseServices {
	/**
	 * add permission path:/permission/addPermission Http:POST
	 * 
	 * @param permission
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@POST
	@Path("/addPermission")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addPermission(Permission permission) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.save(permission);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update Permission information path:/permission/updatePermission Http:PUT
	 * 
	 * @param permission
	 * 
	 * @return Response,ResultPermissionVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":1,"permission":{"id":
	 *         "402881e6445916b401445921eff30003"
	 *         ,"name":"用户注册","type":{"id":"402881e6445916b40144591eb6580001"
	 *         ,"name":"权限","code":"AU0001","parentCode":"DA0001"},"parentCode":
	 *         "P00001"
	 *         ,"url":"/user/registerUser","status":"1","code":"P00002"},
	 *         "list":null}
	 */
	@PUT
	@Path("/updatePermission")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updatePermission(Permission permission) {
		ResultPermissionVo result = new ResultPermissionVo();
		try {
			this.jpaDao.update(permission);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			PermissionVo vo = new PermissionVo();
			vo.setCode(permission.getCode());
			vo.setId(permission.getId());
			vo.setName(permission.getName());
			vo.setParentCode(permission.getParentCode());
			vo.setStatus(permission.getStatus());
			vo.setType(this.getTypeByCode(permission.getTypeCode()));
			vo.setUrl(permission.getUrl());
			result.setPermission(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get permission by id path:/permission/getPermission Http:GET
	 * 
	 * @param permissionId
	 *            primary key of Permission
	 * 
	 * @return Response,ResultPermissionVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":1,"permission":{"id":
	 *         "402881e6445916b401445921eff30003"
	 *         ,"name":"用户注册","type":{"id":"402881e6445916b40144591eb6580001"
	 *         ,"name":"权限","code":"AU0001","parentCode":"DA0001"},"parentCode":
	 *         "P00001"
	 *         ,"url":"/user/registerUser","status":"1","code":"P00002"},
	 *         "list":null}
	 */
	@GET
	@Path("/getPermission")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getPermissionById(
			@QueryParam("permissionId") String permissionId) {
		logger.info("getPermissionById called permissionId:" + permissionId);
		ResultPermissionVo result = new ResultPermissionVo();
		try {
			Permission permission = (Permission) jpaDao.findById(
					Permission.class, permissionId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			PermissionVo vo = new PermissionVo();
			vo.setCode(permission.getCode());
			vo.setId(permission.getId());
			vo.setName(permission.getName());
			vo.setParentCode(permission.getParentCode());
			vo.setStatus(permission.getStatus());
			vo.setType(this.getTypeByCode(permission.getTypeCode()));
			vo.setUrl(permission.getUrl());
			result.setPermission(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete Permission by id path:/permission/deletePermission Http:DELETE
	 * 
	 * @param permisionId
	 *            primary key of Permission
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deletePermission")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deletePermission(
			@QueryParam("permisionId") String permisionId) {
		logger.info("deletePermission called permisionId:" + permisionId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.deleteById(Permission.class, permisionId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * search permission with filter parameters
	 * path:/permission/searchPermission Http:GET
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @param permNameorCode
	 *            the name of permission or code of permission if ignored the
	 *            value is null
	 * 
	 * @return Response, ResultPermissionVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":1,"permission":null,"list"
	 *         :[{"id":"402881e6445916b401445923d0300006"
	 *         ,"name":"修改用户密码","type":
	 *         {"id":"402881e6445916b40144591eb6580001","name"
	 *         :"权限","code":"AU0001"
	 *         ,"parentCode":"DA0001"},"parentCode":"P00001"
	 *         ,"url":"/user/updatePass","status":"1","code":"P00005"}]}
	 */
	@GET
	@Path("/searchPermission")
	@Produces(MediaType.APPLICATION_JSON)
	public Response searchPermission(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("permNameorCode") String permNameorCode) {
		logger.info("searchPermission called json pageNo:" + pageNo
				+ " pageSize:" + pageSize + " permNameorCode:" + permNameorCode);
		String jpql = "from Permission where name=? or code=? order by code asc";
		List<?> list = null;
		ResultPermissionVo result = new ResultPermissionVo();
		List<PermissionVo> lt = new ArrayList<PermissionVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(permNameorCode) || permNameorCode == null) {
			jpql = "from Permission order by code asc";
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, null);
				long totalCount = this.jpaDao.getCount(
						"select count(id) from Permission", null);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		} else {
			Object[] param = { permNameorCode, permNameorCode };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, param);
				long totalCount = this.jpaDao
						.getCount(
								"select count(id) from Permission where name=? or code=?",
								param);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Permission permission = (Permission) o;
				PermissionVo vo = new PermissionVo();
				vo.setCode(permission.getCode());
				vo.setId(permission.getId());
				vo.setName(permission.getName());
				vo.setParentCode(permission.getParentCode());
				vo.setStatus(permission.getStatus());
				vo.setType(this.getTypeByCode(permission.getTypeCode()));
				vo.setUrl(permission.getUrl());
				lt.add(vo);
			}
			result.setList(lt);

		}
		return Response.ok(result).build();
	}

}
